package org.csu.jpetstore.persistence.impl;

import org.csu.jpetstore.domain.Product;
import org.csu.jpetstore.persistence.DBUtil;
import org.csu.jpetstore.persistence.LogDAO;
import org.csu.jpetstore.service.CatalogService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class LogDAOImpl implements LogDAO {

    private static final String INSERT = "INSERT INTO log(user,time,event) VALUES(?,?,?)";
    private static final String SELECT_HISTORY = "select event from log where user = ? and event like '%浏览商品%' order by idnum desc";
    private static final String GET_VIEW_TIME ="select time from log where user = ? and event like ? order by idnum desc limit 1";

    @Override
    public void insert(String username, String time, String event) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, time);
            preparedStatement.setString(3, event);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<String> getHistory(String username) {
        List<String> historyList = new ArrayList<String>();

        try {
            Connection connection = DBUtil.getConnection();
            com.mysql.jdbc.PreparedStatement preparedStatement = (com.mysql.jdbc.PreparedStatement) connection.prepareStatement(SELECT_HISTORY);
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                String id = resultSet.getString(1).substring(4);
                historyList.add(id);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch(Exception e) {
            e.printStackTrace();
        }

        return historyList;
    }

    @Override
    public String getViewTime(String username, Product product) {
        String time = "";
        try {
            Connection connection = DBUtil.getConnection();
            com.mysql.jdbc.PreparedStatement preparedStatement = (com.mysql.jdbc.PreparedStatement) connection.prepareStatement(GET_VIEW_TIME);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2,"%"+product.getProductId()+"%");
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()) {
                time = resultSet.getString(1);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return time;
    }
}
